{% extends 'admin/common/base.html' %}
{% block body %}

<body class="gray-bg">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="role-form">
                    <div class="select-list">
                        <ul>
                            % for c in m.columns:
                            % if c.is_query == 1 and c.html_type == 'datetime' and c.query_type == 'BETWEEN':
                            <li class="select-time">
                                <label>${c.col_comment}：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="${c.col_name}_start"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="${c.col_name}_end"/>
                            </li>
                            % elif c.is_query == 1 and c.html_type == 'datetime':
                            <li class="select-time">
                                <label>${c.col_comment}：</label>
                                <input type="text" class="time-input" id="${c.col_name}" placeholder="开始时间" name="${c.col_name}"/>
                            </li>
                            % elif c.is_query == 1 and c.html_type == 'select' and c.dict_type != '':
                            <li>
                                <label>${c.col_comment}：</label>
                                <select name="${c.col_name}">
                                    <option value="">所有</option>
                                    {% for dd in get_dict_data('${c.dict_type}')%}
                                    <option value="{{dd.value}}">{{dd.label}}</option>
                                    {% endfor %}
                                </select>
                            </li>
                            % elif c.is_query == 1 and c.query_type == 'BETWEEN':
                            <li class="select-time">
                                <label>${c.col_comment}：</label>
                                <input type="text" name="${c.col_name}_start"/>
                                <span>-</span>
                                <input type="text" name="${c.col_name}_end"/>
                            </li>
                            % else:
                            <li>
                                ${c.col_comment}：<input type="text" name="${c.col_name}" />
                            </li>
                            % endif
                            % endfor
                            <li>
                                % if m.tpl_category == 'tree':
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.treeTable.search()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                % else:
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                                % endif
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success {{check_perms('${m.view_path}:${m.model_name}:add')}}" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary single disabled {{check_perms('${m.view_path}:${m.model_name}:edit')}}" onclick="$.operate.edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                % if m.tpl_category == 'tree':
                <a class="btn btn-info" id="expandAllBtn">
                    <i class="fa fa-exchange"></i> 展开/折叠
                </a>
                % else:
                <a class="btn btn-danger multiple disabled {{check_perms('${m.view_path}:${m.model_name}:remove')}}" onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
                % endif
                <a class="btn btn-warning {{check_perms('${m.view_path}:${m.model_name}:export')}}" onclick="$.table.exportExcel()">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="${'bootstrap-tree-table' if m.tpl_category == 'tree' else 'bootstrap-table'}"></table>
            </div>
        </div>
    </div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
<script>
    % if m.tpl_category == 'tree':
    var addFlag = "{{check_perms('${m.view_path}:${m.model_name}:add')}}";
    % endif
    var editFlag = "{{check_perms('${m.view_path}:${m.model_name}:edit')}}";
    var removeFlag = "{{check_perms('${m.view_path}:${m.model_name}:remove')}}";
    % for col in m.get_dict_columns():
    var ${col.col_name}_datas = {{get_dict_data('${col.dict_type}') | tojson | safe}};
    % endfor
    $(function () {
        var options = {
            % if m.tpl_category == 'tree':
            code: "${m.tree_code}",
            parentCode: "${m.tree_parent_code}",
            uniqueId: "id",
            expandAll: false,
            expandFirst: false,
            % endif
            url: "{{url_for('admin.${m.tpl_path}.list')}}",
            createUrl: "{{url_for('admin.${m.tpl_path}.add')}}${'?parent_id={id}' if m.tpl_category == 'tree' else ''}",
            updateUrl: "{{url_for('admin.${m.tpl_path}.edit')}}?id={id}",
            removeUrl: "{{url_for('admin.${m.tpl_path}.remove')}}${'?ids={id}' if m.tpl_category == 'tree' else ''}",
            exportUrl: "{{url_for('admin.${m.tpl_path}.export')}}",
            modalName: "${m.name}",
            columns: [
            % if m.tpl_category == 'tree':
            {
                field: 'selectItem', 
                radio: true
            },
            % else:
            {
                checkbox: true
            },
            {
                field: 'id',
                title: 'ID',
                visible: false
            },
            % endif
            % for c in m.columns:
            % if c.is_list == 1 and c.html_type == 'upload_img':
            {
                field: '${c.col_name}',
                title: '${c.col_comment}',
                formatter: function(value, row, index) {
                    return $.table.imageView(value);
                }
            },
            % elif c.is_list == 1 and c.html_type == 'upload':
            {
                field: '${c.col_name}',
                title: '${c.col_comment}',
                formatter: function(value, row, index) {
                    let html = "<a href='" + value + "' target='_blank'>下载</a>"
                    return html;
                }
            },
            % elif c.is_list ==1 and (c.html_type == 'select' or c.html_type == 'radio'):
            {
                field: '${c.col_name}',
                title: '${c.col_comment}',
                formatter: function(value, row, index) {
                    return $.table.selectDictLabel(${c.col_name}_datas, value);
                }
            },
            % elif c.is_list == 1 and c.html_type == 'textarea':
            {
                field: '${c.col_name}',
                title: '${c.col_comment}',
                formatter: function(value, row, index) {
                    return $.table.tooltip(value, 10, "open");
                }
            },
            % elif c.is_list == 1:
            {
                field: '${c.col_name}',
                title: '${c.col_comment}'
            },
            % endif
            % endfor
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                    % if m.tpl_category == 'tree':
                    actions.push('<a class="btn btn-info btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="$.operate.add(\'' + row.id + '\')"><i class="fa fa-plus"></i>新增</a> ');
                    % endif
                    actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                    return actions.join('');
                }
            }]
        };
        % if m.tpl_category == 'tree':
        $.treeTable.init(options);
        % else:
        $.table.init(options);
        % endif
    });

</script>
{% endblock %}